package com.jyh.blog.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jyh.blog.dao.AlbumDao;
import com.jyh.blog.dao.PhotoDao;
import com.jyh.blog.dto.AlbumBackDTO;
import com.jyh.blog.dto.AlbumDTO;
import com.jyh.blog.entity.Album;
import com.jyh.blog.entity.Photo;
import com.jyh.blog.exception.MyException;
import com.jyh.blog.service.AlbumService;
import com.jyh.blog.utils.BeanCopyUtils;
import com.jyh.blog.utils.PageUtils;
import com.jyh.blog.vo.AlbumVO;
import com.jyh.blog.vo.ConditionVO;
import com.jyh.blog.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.List;

import static com.jyh.blog.constant.CommonConst.FALSE;
import static com.jyh.blog.enums.ArticleStatusEnum.PUBLIC;

/**
 * (Album)表服务实现类
 *
 * @author jyh
 * @since 2023-02-12 15:37:28
 */
@Service("albumService")
public class AlbumServiceImpl extends ServiceImpl<AlbumDao, Album> implements AlbumService {
    @Autowired
    private AlbumDao albumDao;
    @Autowired
    private PhotoDao photoDao;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveOrUpdateAlbum(AlbumVO albumVO) {
        //判断相册名是否存在
        Album album = this.getOne(new LambdaQueryWrapper<Album>().select(Album::getId).eq(Album::getName, albumVO.getName()));
        //相册存在，且查询到相册名存在，且两者id不同（防止相册id不同名字一样的情况）
        if (!ObjectUtils.isEmpty(album) && !ObjectUtils.isEmpty(album.getId()) && !album.getId().equals(albumVO.getId())) {
            throw new MyException("相册名重复");
        }
        album = BeanCopyUtils.copyObject(albumVO, Album.class);
        this.saveOrUpdate(album);
    }

    @Override
    public PageResult<AlbumBackDTO> albumBackList(ConditionVO conditionVO) {
        String keywords = conditionVO.getKeywords();
        //查询相册数量
        Integer count = albumDao.selectCount(new LambdaQueryWrapper<Album>().select(Album::getId)
                .like(StringUtils.hasText(keywords),Album::getName, keywords));
        if(count==0){
            return new PageResult<>();
        }
        List<AlbumBackDTO> records = albumDao.albumBackList(PageUtils.getLimitCurrent(),PageUtils.getSize(),keywords);
        return new PageResult(records,count);
    }

    @Override
    public AlbumBackDTO getAlbumById(String id) {
        //通过相册id查询图片数量
        Integer count = photoDao.selectCount(new LambdaQueryWrapper<Photo>().select(Photo::getId).eq(Photo::getAlbumId, id));
        Album album = this.getById(id);
        if(ObjectUtils.isEmpty(album)){
            throw new MyException("相册不存在");
        }
        AlbumBackDTO albumBackDTO = BeanCopyUtils.copyObject(album, AlbumBackDTO.class);
        albumBackDTO.setPhotoCount(count);
        return albumBackDTO ;
    }

    @Override
    public void deleteById(String id) {
        //通过相册id查询图片数量
        Integer count = photoDao.selectCount(new LambdaQueryWrapper<Photo>().select(Photo::getId).eq(Photo::getAlbumId, id));
        //相册里有图片不能删除
        if(count>0){
            throw new MyException("请先删除相册中的图片");
        }
        this.removeById(id);
    }

    @Override
    public List<AlbumBackDTO> getAlbumInfo() {
        List<Album> list = this.list();
        return BeanCopyUtils.copyList(list, AlbumBackDTO.class);
    }

    @Override
    public List<AlbumDTO> albumList() {
        List<Album> albumList = this.list(new LambdaQueryWrapper<Album>()
                .select(Album::getId, Album::getName, Album::getCover, Album::getDescription)
                .eq(Album::getStatus, PUBLIC.getStatus())
                .eq(Album::getIs_delete, FALSE)
        );
        return BeanCopyUtils.copyList(albumList,AlbumDTO.class);
    }
}

